// --- Mock Data Options ---
const getOptionsData = () => ({
    genderOptions: ['男', '女', '保密'],
    doctorOptions: ['魏医生 (本人)', '王医生', '陈医生'],
    sourceOptions: ['转介绍', '线上平台', '地推活动', '自然到院'],
  });
  
  // --- Initial Registration Data Structure ---
  const getInitialRegistrationData = () => ({
    name: '',
    gender: '',
    birthday: '',
    phone: '',
    doctor: '',
    source: '',
    remark: '',
  });
  
  Page({
    data: {
      registrationData: getInitialRegistrationData(),
      genderOptions: getOptionsData().genderOptions,
      doctorOptions: getOptionsData().doctorOptions,
      sourceOptions: getOptionsData().sourceOptions,
      currentDate: new Date().toISOString().split('T')[0], // 用于日期选择器上限
      genderIndex: -1,
      doctorIndex: -1,
      sourceIndex: -1,
    },
  
    // --- Input Handlers ---
    inputName(e) { this.setData({'registrationData.name': e.detail.value}); },
    inputPhone(e) { this.setData({'registrationData.phone': e.detail.value}); },
    inputRemark(e) { this.setData({'registrationData.remark': e.detail.value}); },
  
    // --- Picker Handlers ---
    bindGenderChange(e) {
      const index = e.detail.value;
      this.setData({
        genderIndex: index,
        'registrationData.gender': this.data.genderOptions[index]
      });
    },
    bindDateChange(e) {
      this.setData({'registrationData.birthday': e.detail.value});
    },
    bindDoctorChange(e) {
      const index = e.detail.value;
      this.setData({
        doctorIndex: index,
        'registrationData.doctor': this.data.doctorOptions[index]
      });
    },
    bindSourceChange(e) {
      const index = e.detail.value;
      this.setData({
        sourceIndex: index,
        'registrationData.source': this.data.sourceOptions[index]
      });
    },
  
    // --- Submission ---
    submitRegistration() {
      const data = this.data.registrationData;
      
      if (!data.name || !data.gender || !data.birthday || !data.doctor) {
        wx.showToast({ title: '姓名、性别、生日、主治医为必填项！', icon: 'none' });
        return;
      }
  
      // 实际业务：调用后端 API 提交登记数据
      // const newPatientId = await api.createPatient(data); 
  
      console.log('--- 提交患者登记数据 ---', data);
      
      wx.showModal({
        title: '登记成功',
        content: `患者 ${data.name} 档案已创建。是否立即查看档案？`,
        success: (res) => {
          if (res.confirm) {
            // 跳转到 患者档案页
            // wx.redirectTo({ url: `/pages/patient-profile/patient-profile?id=${newPatientId}` });
            wx.redirectTo({ url: '/pages/doctor/patient-profile/patient-profile?id=P01555' }); // Mock 跳转
          } else {
            wx.navigateBack(); // 返回患者列表
          }
        }
      });
    }
  });